package ga.individuals;

import com.mathworks.toolbox.javabuilder.MWException;
import com.mathworks.toolbox.javabuilder.MWNumericArray;

public class Individual implements Comparable<Individual> {
	private MWNumericArray individualValue;
	private Double fitnessValue;
	
	public Individual(MWNumericArray individualValue) throws MWException {
		this.individualValue = individualValue;
		//this.fitnessValue = 1.0d / Fitness.getFitnessValue(this);
		this.fitnessValue = -1.0d * Fitness.getFitnessValue(this);
	}

	
	public MWNumericArray getIndividualValue() {
		return individualValue;
	}
	
	public void setInvididualValue(MWNumericArray individualValue) throws MWException {
		this.individualValue = individualValue;
		this.fitnessValue = Fitness.getFitnessValue(this);
	}

	public Double getFitnessValue() {
		return fitnessValue;
	}

	public void setFitnessValue(Double fitnessValue) {
		this.fitnessValue = fitnessValue;
	}
	
	@Override
	public int compareTo(Individual otherIndividual) {
		if (this.fitnessValue - otherIndividual.fitnessValue < 0) {
			return -1;
		} else if (this.fitnessValue == otherIndividual.fitnessValue) {
			return 0;
		}
		return 1;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((fitnessValue == null) ? 0 : fitnessValue.hashCode());
		result = prime * result
				+ ((individualValue == null) ? 0 : individualValue.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Individual other = (Individual) obj;
		if (fitnessValue == null) {
			if (other.fitnessValue != null)
				return false;
		} else if (!fitnessValue.equals(other.fitnessValue))
			return false;
		if (individualValue == null) {
			if (other.individualValue != null)
				return false;
		} else if (!individualValue.equals(other.individualValue))
			return false;
		return true;
	}

	@Override
	public String toString() {
		//return "Individual [individualValue=" + individualValue
		//		+ ", fitnessValue=" + fitnessValue + "]";
		return "Individual fitness: " + fitnessValue;
		
	}

}
